package demo2;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.tools.ant.Project;
import org.apache.tools.ant.taskdefs.Zip;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;

public class Demo2 {

	public static void main(String[] args) {
		
		zip3("D:\\mail","C:/aaa.zip");
	}
	
	public static void  zip(String dest,String src){
		Zip zip = new Zip();
		zip.setBasedir(new File(src));
		//zip.setIncludes(...); 包括哪些文件或文件夹eg:zip.setIncludes("*.java");
		//zip.setExcludes(...); 排除哪些文件或文件夹
		zip.setDestFile(new File(dest));
		Project p = new Project();
		p.setBaseDir(new File(src));
		zip.setProject(p);
		zip.execute();
		}
   public static void zip2(){

   	try {
   		long b = System.currentTimeMillis();
			String outPutZip = "C:\\a.zip";
			ZipOutputStream out = new ZipOutputStream(new FileOutputStream(outPutZip));
			out.setEncoding("GB2312");
			String []localFile = {"D:\\敬亭山.docx","C:\\快盘\\敬亭山.docx"};
			
			for(int i=0; i<localFile.length; i++)
			{
				File currnetFile = new File(localFile[i]);
				String filename= currnetFile.getName();
				System.out.println("localFile[i]:"+filename);
				FileInputStream fis = new FileInputStream(localFile[i]);
				out.putNextEntry(new ZipEntry(filename));
				int len;
			    byte[] buffer = new byte[1024];
			    while((len = fis.read(buffer))>0)
			    {
			  		out.write(buffer,0,len);
			    }
			}
			out.closeEntry();
			out.close();
			long e = System.currentTimeMillis();
			System.out.println(e-b);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
   
   
   }
   
   
   /**  
    *  
    * @param file 要压缩的文件  
    * @param zipFile 压缩文件存放地方  
    */  
   public static void zip3(String filepath, String zipFilepath) {  
	   File file = new File(filepath);
	   File zipFile  = new File(zipFilepath);
	   
       ZipOutputStream outputStream = null;  
       try {  
           outputStream = new ZipOutputStream(new FileOutputStream(zipFile));  
           zipFile(outputStream, file, "");  
           if (outputStream != null) {  
               outputStream.flush();  
               outputStream.close();  
           }  
       } catch (IOException ex) {  
    	   ex.printStackTrace();
       } finally {  
           try {  
               outputStream.close();  
           } catch (IOException ex) {  
        	   ex.printStackTrace();
           }  
       }  
   }  
 
   /**  
    *  
    * @param output ZipOutputStream对象  
    * @param file 要压缩的文件或文件夹  
    * @param basePath 条目根目录  
    */  
   private static void zipFile(ZipOutputStream output, File file, String basePath) {  
       FileInputStream input = null;  
       try {  
           // 文件为目录  
           if (file.isDirectory()) {  
               // 得到当前目录里面的文件列表  
               File list[] = file.listFiles();  
               basePath = basePath + (basePath.length() == 0 ? "" : "/")  
                       + file.getName();  
               System.out.println("dir: " + basePath);
               // 循环递归压缩每个文件  
               for (File f : list) {  
                   zipFile(output, f, basePath);  
               }  
           } else {  
               // 压缩文件  
        	   System.out.println("file: "+basePath);
               basePath = (basePath.length() == 0 ? "" : basePath + "/")  
                       + file.getName();  
               // System.out.println(basePath);  
               output.putNextEntry(new ZipEntry(basePath));  
               input = new FileInputStream(file);  
               int readLen = 0;  
               byte[] buffer = new byte[1024 * 8];  
               while ((readLen = input.read(buffer, 0, 1024 * 8)) != -1) {  
                   output.write(buffer, 0, readLen);  
               }  
           }  
       } catch (Exception ex) {  
           ex.printStackTrace();  
       } finally {  
           // 关闭流  
           if (input != null) {  
               try {  
                   input.close();  
               } catch (IOException ex) {  
            	   ex.printStackTrace();
               }  
           }  
       }  
   } 
   
}
